import { getTagData, getDatasetData} from "../../../utils/util"


Page({

    /**
     * 页面的初始数据
     */
    data: {
        colorPrimary: wx.$theme.color,
        halfScreenButtons: [{ type: 'default', text: '取消', className: 'dialogCancel' }, {type: 'primary',  text: '确定', className: 'dialogConfirm' }],
        formData:  {},
    },

    inputChange(e) {
      const {field} = getDatasetData(e)
      this.setData({
          [`formData.${field}`]: e.detail.value
      })
  },
  bindblur: function(e) {
      setTimeout(() => {
          const {field} = e.mark;
          this.setData({[`edit.${field}`]: false});
      }, 3000)
  },
  inputfocus: function (e) {
      const {field} = e.mark;
      this.setData({[`edit.${field}`]: true});
  },
    /**保存名称 */
    tapSave: function(e) {
      const {field} = e.mark;
      const {deviceCloudId, formData} = this.data;
      wx.$http.put({url: '/szm/devicecloud', data: {id: deviceCloudId, [field]: formData[field]}}).then(res => {
          this.setData({[`edit.${field}`]: false});
          wx.showToast({
              title: '保存成功',
          });
          this.initData();
      });
  },

    /**分组 */
    tapGroup() {
        this.setData({
            [`halfScreen.show`]: !0
        })
    },

        /**授权 */
        tapAvaibale: function(e) {
          const {value} = e.detail;
          const {device} = this.data;
          wx.$http.put({url: `/szm/devicecloud`, data: {id: device.id, available: value ? 0 : 1}}).then(res => {
            this.initData();
          });
      },

      tapLockTime: function() {
        const {device} = this.data;
        const _this = this;
        wx.navigateTo({
          url: `../schedule/index?lockTimeAvailable=${device.lockTimeAvailable}&lockTime=${device.lockTime}`,
          events: {
            scheduleEvent: function(data) {
              const {lockTimeAvailable, lockTime} = data;
              wx.$http.put({url: `/szm/devicecloud`, data: {id: device.id, lockTimeAvailable, lockTime}}).then(res => {
                _this.initData();
              });
            }
          }
        })
      },

    initData() {
      const {deviceCloudId} = this.data
      return wx.$http.get({url: `/szm/devicecloud/${deviceCloudId}`}).then(res => {
        const {data} = res;
        const lockTimeText = data.lockTime > 0 ? new Date(Number(data.lockTime)).Format() + (data.lockTimeAvailable ? ' 未启用' : ' 定时中') : "";
        const groupName = data.placeName ? (data.placeName + (data.roomName ? ('-' + data.roomName) : '')) : '未分组'
        this.setData({
            device: Object.assign({groupName, lockTimeText}, data),
        });
        return res;
      })
  },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        const {id} = options
        this.setData({deviceCloudId: id});
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
        
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
      this.initData().then(() => {
        this.getPlaceRoomDTO();
      });
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },


     /**关闭半屏弹窗回调 */
     tapHalfButton: function(e) {
		this.setData({
            [`halfScreen.show`]: !1
        })
        const {deviceCloudId, halfScreen} = this.data;
        const placeId = halfScreen.list0[halfScreen.index[0]].id;
        const roomId = halfScreen.list1[halfScreen.index[1]] ? halfScreen.list1[halfScreen.index[1]].id : 0;
        wx.$http.put({url: '/szm/devicecloud', data: {id: deviceCloudId, placeId, roomId}}).then(res => {
            wx.showToast({
                title: '分组',
            });
            this.initData();
        });
        
    },

    halfScreenChange: function(e) {
        const values = e.detail.value;
        this.setData({
            [`halfScreen.index`]: values
        })
        this.setData({
            [`halfScreen.list1`]: this.data.halfScreen.list0[values[0]].child
        })
    },
    getPlaceRoomDTO() {
        wx.$http.get({url: '/szm/place/actionSheet'}).then(res => {
            if (res.data && res.data.length > 0) {
                const list0 = res.data;
                let list1 = res.data[0].child || [];
                const {placeId, roomId} = this.data.device;
                const indexs = [0, 0];
                if (parseInt(roomId) > 0) {
                    const index0 = list0.findIndex(l => l.id === placeId);
                    list1 = list0[index0].child;
                    const index1 = list1 && list1.length > 0 ? list1.findIndex(l => l.id === roomId) : 0;
                    indexs[0] = index0;
                    indexs[1] = index1;
                }
                this.setData({
                    [`halfScreen.list0`]: list0,
                    [`halfScreen.list1`]: list1
                });
    
                this.setData({
                    [`halfScreen.index`]: indexs
                })
            }
        });
    },

    hideLoading: function (params) {
        
    }
})